﻿namespace AutoMail.Log.QuartzLog
{
    public class QuartzLogger
    {
        enum LogType
        {
            TopShelf,
            Quartz,
            User
        }

        private static Dictionary<LogType, QuartzLogger> _dicLogInstances = new Dictionary<LogType, QuartzLogger>();

        private static string topshelfLogFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TopshelfLog.txt");
        private static string quartzLogFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "QuartzLog.txt");
        private static string userLogFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UserLog.txt");

        public static void LogTopshelfInfo(string logString)
        {
            lock (_dicLogInstances)
            {
                if (!_dicLogInstances.ContainsKey(LogType.TopShelf))
                {
                    _dicLogInstances[LogType.TopShelf] = new QuartzLogger();
                }

                _dicLogInstances[LogType.TopShelf].LogToATxtFile(topshelfLogFilename, logString);
            }
        }

        public static void LogQuartzInfo(string logString)
        {
            lock (_dicLogInstances)
            {
                if (!_dicLogInstances.ContainsKey(LogType.Quartz))
                {
                    _dicLogInstances[LogType.Quartz] = new QuartzLogger();
                }

                _dicLogInstances[LogType.Quartz].LogToATxtFile(quartzLogFilename, logString);
            }
        }

        public static void LogUserInfo(string logString)
        {
            lock (_dicLogInstances)
            {
                if (!_dicLogInstances.ContainsKey(LogType.User))
                {
                    _dicLogInstances[LogType.User] = new QuartzLogger();
                }

                _dicLogInstances[LogType.User].LogToATxtFile(userLogFilename, logString);
            }
        }

        private void LogToATxtFile(string fileName, string logString)
        {
            using (StreamWriter sw = File.AppendText(fileName))
            {
                sw.WriteLine(logString);
                sw.Flush();
            }
        }
    }
}
